package com.hutao.manage.controller;


import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.hutao.manage.component.Config;
import com.hutao.manage.pojo.CitizensInfo;
import com.hutao.manage.pojo.FirstLevelMedical;
import com.hutao.manage.pojo.HospitalizationNote;
import com.hutao.manage.pojo.LeftMenuPojo;
import com.hutao.manage.query.MedBindQuery;
import com.hutao.manage.service.BasicInfoService;
import com.hutao.manage.service.MedBindService;
import com.hutao.manage.util.CookieUtils;
import com.hutao.manage.util.DateUtil;
import com.hutao.manage.util.JsonUtils;
import com.hutao.manage.util.Params;
import com.hutao.manage.util.Result;

/**
 * 病案绑定Controller
 * @author Administrator
 *
 */
@Controller
@SessionAttributes("current_user")
@RequestMapping("/bind")
public class MedBindController {

	@Autowired
	private MedBindService medBindService;
	
	@Autowired
	private BasicInfoService basicInfoService;
	

	@Autowired  
    private Config conf; 
	/*//病案绑定
	@RequestMapping("/medBind")
	public String medBind(Params params,Model model){
		List<Map<String,Object>> unBindList=medBindService.getUnBindList(params);
		int countSize=medBindService.getUnBindCount(params);
		model.addAttribute("unBindList", unBindList);
		model.addAttribute("unBindSize", countSize);
		
		List<Map<String,Object>> bindList=medBindService.getBindList(params);
		int countSize=medBindService.getUnBindCount(params);
		model.addAttribute("bindList", bindList);
		model.addAttribute("unBindSize", countSize);
		String bindListJson = JsonUtils.objectToJson(bindList);
		model.addAttribute("bindListJson", bindListJson);
		return "medbind/Pigeonhole_list";
	}*/
	
	@RequestMapping("/medBind")
	public String medBind(MedBindQuery params,Model model,boolean query){
		/*List<Map<String,Object>> unBindList=medBindService.getUnBindList(params);
		int countSize=medBindService.getUnBindCount(params);
		model.addAttribute("unBindList", unBindList);
		model.addAttribute("unBindSize", countSize);*/
		LeftMenuPojo leftMenu = LeftMenuPojo.GetLeftMenu("upload_module");
		String leftMenuJson = JsonUtils.objectToJson(leftMenu);
		List<Map<String,Object>> bindList = new ArrayList<>();
		int countSize = 0;
		if(!query){
			params.setBindStatus(0);
		}
		if(params.isQuery()){
			 bindList=medBindService.getBindList(params);
			 countSize=medBindService.getBindCount(params);
			 params.setTotalCounts(countSize);
		}
		String bindListJson = JsonUtils.objectToJson(bindList);
		model.addAttribute("bindListJson", bindListJson);
		model.addAttribute("pageJson", JsonUtils.objectToJson(params));
		model.addAttribute("leftMenuJson", leftMenuJson);
		model.addAttribute("medBindQuery", params);
		return "medbind/Pigeonhole_list.page";
	}
	
	//病案绑定
/*	@ResponseBody
	@RequestMapping("/getMedData")
	public List<Map<String,Object>> medBind(Params params){
		
		
		return medBindService.getUnBindList(params);
	}*/
	
	//病案绑定
	@RequestMapping("/pigeonHoleList")
	public String getFileUrlList(Integer id,Model model){
		
		List<Map<String,Object>> urlList=medBindService.fileUrlList(id);
		model.addAttribute("urlList", urlList);
		model.addAttribute("uploadScanId",id);
		String resultJson = JsonUtils.objectToJson(urlList);
		model.addAttribute("resultJson", resultJson);
		model.addAttribute("urlCount", urlList.size());
		model.addAttribute("hospitalizationNoteResourceUrl", conf.getHospitalizationNoteResourceUrl());
		
		return "medbind/Pigeonhole_bind";
	}

	
	//病案绑定
	@RequestMapping("/addPatientPage")
	public String addPatientPage(Integer id,Model model){
		//上传图片信息
		List<Map<String,Object>> urlList=medBindService.fileUrlList(id);
		Map<String, List<Map<String,Object>>> basicInfos = basicInfoService.getBasicInfos();
		model.addAttribute("urlList", urlList);
		model.addAttribute("basicInfos", basicInfos);
		model.addAttribute("count", urlList.size());
		
		model.addAttribute("uploadScanId", id);
		//婚姻字典信息
		List<Map<String,Object>> marriageMsgList=medBindService.getMarriageList();
		model.addAttribute("marriageMsgList", marriageMsgList);
				
	    //就诊类型
		List<Map<String,Object>> treatmentTypeList=medBindService.getTreatmentTypeList();
		model.addAttribute("treatmentTypeList", treatmentTypeList);	
						
		//就诊级别
		List<Map<String,Object>> treatmentSignList=medBindService.getTreatmentSignList();
		model.addAttribute("treatmentSignList", treatmentSignList);		
				
		//医院信息
		List<Map<String,Object>> hospitalMsgList=medBindService.getHospitalList();
		model.addAttribute("hospitalMsgList", hospitalMsgList);
		
		model.addAttribute("hospitalizationNoteResourceUrl", conf.getHospitalizationNoteResourceUrl());
		
		return "medbind/addPatient";
	}
	
	/**
	 * 添加患者信息
	 * @param citizensInfo
	 * 		公民信息
	 * @param firstLevelMedical
	 * 		一级诊疗
	 * @param hospitalizationNote
	 * 		住院信息
	 * @return
	 */
	@RequestMapping("/addPatient")
	public String addPatient(CitizensInfo citizensInfo,FirstLevelMedical firstLevelMedical,HospitalizationNote hospitalizationNote,Integer uploadScanId,Model model,HttpServletRequest request){
		System.out.println(citizensInfo+"--"+firstLevelMedical);

		String currentUser = CookieUtils.getCookieValue(request, "TT_TOKEN");
		medBindService.addPatient(citizensInfo, firstLevelMedical, hospitalizationNote,uploadScanId,currentUser);
		
		return "redirect:/medrecord/showMedrecordImage?firstGuid="+firstLevelMedical.getGuid();
		/*return addPatientPage(uploadScanId,model);*/
	}
	
	/**
	 * 通过姓名和身份证信息获取患者信息
	 * @param params
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/getPatientMsg")
	public  Result  getPatientMsg(Params params){
		try {
			List<Map<String,Object>> patientMsg= medBindService.getPatientMsg(params);
			if(patientMsg!=null && patientMsg.size()>0){
				return new Result(true, "查询患者信息成功", patientMsg);
				
			}else{
				return new Result(true, "查询患者信息不存在", null);
			}
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(false, "查询患者信息出现异常", null);
		}
	}
	
	
	@ResponseBody
	@PostMapping("/bind_medical_record_image")
	public Result bindMedicalRecordImage(String mrId,Integer visitNumber,HttpServletRequest request){
		try {
			
			String currentUser = CookieUtils.getCookieValue(request, "TT_TOKEN");
			medBindService.bindMedicalRecordImage(mrId, visitNumber, currentUser);
			return new Result(true, "绑定成功!");
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(false, "绑定失败!");
		}
	}
	
	
	@ResponseBody
	@RequestMapping("/bindUrl")
	public Result bindUrl(String guid,Integer uploadScanId,HttpServletRequest request){
		try {
			boolean isHave=medBindService.isHaveMedResource(guid);
			if(isHave){
				return new Result(false, "诊疗信息和相关数据已经绑定!请勿再次绑定!");
			}
			String currentUser = CookieUtils.getCookieValue(request, "TT_TOKEN");
			medBindService.bindUrlByFileHashAndUploadScan(guid, uploadScanId,currentUser);
			return new Result(true, "患者和资源信息绑定成功!");
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(false, "患者和资源信息绑定失败!");
		}
	}
	
	@RequestMapping("/addMedicalPage")
	public String addMedicalPage(Integer uploadScanId,Model model,String citizenGuid) throws ParseException, Exception{
		CitizensInfo citizen=medBindService.getCitizenByGuid(citizenGuid);
		
		if(citizen!=null && citizen.getBirthday() != null){
			model.addAttribute("birthday", DateUtil.DateToStr("yyyy-MM-dd", citizen.getBirthday()));
		}
		model.addAttribute("citizen", citizen);
		model.addAttribute("uploadScanId", uploadScanId);
		Map<String, List<Map<String,Object>>> basicInfos = basicInfoService.getBasicInfos();
		model.addAttribute("basicInfos", basicInfos);
		//上传图片信息
		List<Map<String,Object>> urlList=medBindService.fileUrlList(uploadScanId);
		model.addAttribute("urlList", urlList);
		model.addAttribute("count", urlList.size());
				
		//性别字典信息
		List<Map<String,Object>> sexMsgList=medBindService.getSexDicMsgList();
		model.addAttribute("sexMsgList", sexMsgList);
						
		//婚姻字典信息
		List<Map<String,Object>> marriageMsgList=medBindService.getMarriageList();
		model.addAttribute("marriageMsgList", marriageMsgList);
						
		//就诊类型
		List<Map<String,Object>> treatmentTypeList=medBindService.getTreatmentTypeList();
		model.addAttribute("treatmentTypeList", treatmentTypeList);	
				
		//就诊级别
		List<Map<String,Object>> treatmentSignList=medBindService.getTreatmentSignList();
		model.addAttribute("treatmentSignList", treatmentSignList);		
						
		//医院信息
		List<Map<String,Object>> hospitalMsgList=medBindService.getHospitalList();
		model.addAttribute("hospitalMsgList", hospitalMsgList);
		
		model.addAttribute("hospitalizationNoteResourceUrl", conf.getHospitalizationNoteResourceUrl());
		
		
		return "medbind/addMedical";
	}
	
	@RequestMapping("/addMedical")
	public String addPatient(FirstLevelMedical firstLevelMedical,Model model,HospitalizationNote hospitalizationNote,String citizenGuid,Integer uploadScanId,HttpServletRequest request) throws ParseException, Exception{
		String currentUser = CookieUtils.getCookieValue(request, "TT_TOKEN");
		
		medBindService.addMedical(firstLevelMedical, hospitalizationNote,citizenGuid,uploadScanId,currentUser);
		/*return addMedicalPage(uploadScanId,model,citizenGuid);*/
		return "redirect:/medrecord/showMedrecordImage?firstGuid="+firstLevelMedical.getGuid();
	}
	
	@ResponseBody
	@RequestMapping("/getInOutHospitalType")
	public Result getInOutHospitalType(Integer hospitalId){
		if(hospitalId!=null){
			Map<String,Object> objects = new HashMap<String,Object>();
			//入院方式
			List<Map<String,Object>> inHospitalTypeList=medBindService.getInHospitalTypeList(hospitalId);
			
			//出院方式
			List<Map<String,Object>> outHospitalTypeList=medBindService.getOutHospitalTypeList(hospitalId);
			//医疗科室
			List<Map<String,Object>> medicalOfficeList=medBindService.getMedicalOfficeList(hospitalId);
			objects.put("inHospitalTypeList", inHospitalTypeList);
			objects.put("outHospitalTypeList",outHospitalTypeList);
			objects.put("medicalOfficeList",medicalOfficeList);
			return new Result(true, "获取出入院方式信息成功", objects);
		}else{
			return new Result(false, "获取出入院方式信息所需的医院编号为空");
		}
			
	}
	
	@RequestMapping("/unPigeonhole")
	public String unPigeonhole(MedBindQuery params,Model model){
		/*List<Map<String,Object>> unBindList=medBindService.getUnBindList(params);
		int countSize=medBindService.getUnBindCount(params);
		model.addAttribute("unBindList", unBindList);
		model.addAttribute("unBindSize", countSize);*/
		LeftMenuPojo leftMenu = LeftMenuPojo.GetLeftMenu("upload_module");
		String leftMenuJson = JsonUtils.objectToJson(leftMenu);
		List<Map<String,Object>> medicalOffices = basicInfoService.getMedicalOfficesByHospitalId(1);

		List<Map<String,Object>> bindList = new ArrayList<>();
		int countSize = 0;
		params.setBindStatus(1);
		if(params.isQuery()){
			 bindList=medBindService.getBindList(params);
			 countSize=medBindService.getBindCount(params);
			 params.setTotalCounts(countSize);
		}
		String bindListJson = JsonUtils.objectToJson(bindList);
		model.addAttribute("bindListJson", bindListJson);
		model.addAttribute("pageJson", JsonUtils.objectToJson(params));
		model.addAttribute("leftMenuJson", leftMenuJson);
		model.addAttribute("medBindQuery", params);
		model.addAttribute("offices", medicalOffices);
		return "medbind/unPigeonhole.page";
	}
	
	
	@RequestMapping("/unPigeonhole1")
	public String unPigeonhole1(MedBindQuery params,Model model){
		/*List<Map<String,Object>> unBindList=medBindService.getUnBindList(params);
		int countSize=medBindService.getUnBindCount(params);
		model.addAttribute("unBindList", unBindList);
		model.addAttribute("unBindSize", countSize);*/
		LeftMenuPojo leftMenu = LeftMenuPojo.GetLeftMenu("upload_module");
		String leftMenuJson = JsonUtils.objectToJson(leftMenu);
		List<Map<String,Object>> medicalOffices = basicInfoService.getMedicalOfficesByHospitalId(1);

		List<Map<String,Object>> bindList = new ArrayList<>();
		int countSize = 0;
		params.setBindStatus(1);
		if(params.isQuery()){
			 bindList=medBindService.getBindList(params);
			 countSize=medBindService.getBindCount(params);
			 params.setTotalCounts(countSize);
		}
		String bindListJson = JsonUtils.objectToJson(bindList);
		model.addAttribute("bindListJson", bindListJson);
		model.addAttribute("pageJson", JsonUtils.objectToJson(params));
		model.addAttribute("leftMenuJson", leftMenuJson);
		model.addAttribute("medBindQuery", params);
		model.addAttribute("offices", medicalOffices);
		return "medbind/unPigeonhole1.page";
	}
	
	@RequestMapping("/pigeonholed")
	public String unPigeonholed(MedBindQuery params,Model model){
		/*List<Map<String,Object>> unBindList=medBindService.getUnBindList(params);
		int countSize=medBindService.getUnBindCount(params);
		model.addAttribute("unBindList", unBindList);
		model.addAttribute("unBindSize", countSize);*/
		LeftMenuPojo leftMenu = LeftMenuPojo.GetLeftMenu("upload_module");
		String leftMenuJson = JsonUtils.objectToJson(leftMenu);
		List<Map<String,Object>> medicalOffices = basicInfoService.getMedicalOfficesByHospitalId(1);

		List<Map<String,Object>> bindList = new ArrayList<>();
		int countSize = 0;
		params.setBindStatus(1);
		if(params.isQuery()){
			 bindList=medBindService.getBindList(params);
			 countSize=medBindService.getBindCount(params);
			 params.setTotalCounts(countSize);
		}
		String bindListJson = JsonUtils.objectToJson(bindList);
		model.addAttribute("bindListJson", bindListJson);
		model.addAttribute("pageJson", JsonUtils.objectToJson(params));
		model.addAttribute("leftMenuJson", leftMenuJson);
		model.addAttribute("medBindQuery", params);
		model.addAttribute("offices", medicalOffices);
		return "medbind/pigeonholed.page";
	}
}
